package com.yami.shop.seckill.multishop.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.yami.shop.common.exception.YamiShopBindException;
import com.yami.shop.common.response.ServerResponseEntity;
import com.yami.shop.common.util.PageParam;
import com.yami.shop.seckill.common.model.SeckillOrder;
import com.yami.shop.seckill.common.service.SeckillOrderService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springdoc.api.annotations.ParameterObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.Objects;

/**
 * @author LGH
 * @date 2019-08-28 09:36:59
 */
@RestController
@RequestMapping("/seckill/seckillOrder")
@Tag(name = "商家端秒杀订单接口")
public class SeckillOrderController {

    @Autowired
    private SeckillOrderService seckillOrderService;

    @GetMapping("/page")
    @Operation(summary =  "分页查找秒杀订单信息")
    public ServerResponseEntity<IPage<SeckillOrder>> getSeckillOrderPage(PageParam<SeckillOrder> page,@ParameterObject SeckillOrder seckillOrder) {
        return ServerResponseEntity.success(seckillOrderService.page(page, new LambdaQueryWrapper<SeckillOrder>()));
    }

    @GetMapping("/info/{seckillOrderId}")
    @Operation(summary =  "通过id查询秒杀订单")
    @Parameter(name = "seckillOrderId", description = "秒杀订单id", required = true)
    public ServerResponseEntity<SeckillOrder> getById(@PathVariable("seckillOrderId") Long seckillOrderId) {
        return ServerResponseEntity.success(seckillOrderService.getById(seckillOrderId));
    }

    @PostMapping
    @Operation(summary =  "新增秒杀订单")
    public ServerResponseEntity<Boolean> save(@RequestBody @Valid SeckillOrder seckillOrder) {
        return ServerResponseEntity.success(seckillOrderService.save(seckillOrder));
    }

    @PutMapping
    @Operation(summary =  "修改秒杀订单")
    public ServerResponseEntity<Boolean> updateById(@RequestBody @Valid SeckillOrder seckillOrder) {
        return ServerResponseEntity.success(seckillOrderService.updateById(seckillOrder));
    }

    @DeleteMapping("/{seckillOrderId}")
    @Operation(summary =  "通过id删除秒杀订单")
    @Parameter(name = "seckillOrderId", description = "秒杀订单id", required = true)
    public ServerResponseEntity<Boolean> removeById(@PathVariable Long seckillOrderId) {
        return ServerResponseEntity.success(seckillOrderService.removeById(seckillOrderId));
    }

    @GetMapping("/getSeckillIdByOrderNumber")
    @Operation(summary =  "根据订单编号获取秒杀id")
    @Parameter(name = "orderNumber", description = "订单编号", required = true)
    public ServerResponseEntity<Long> getSeckillIdByOrderNumber(@RequestParam("orderNumber") String orderNumber) {
        SeckillOrder seckillOrder = seckillOrderService.getOne(new LambdaQueryWrapper<SeckillOrder>().eq(SeckillOrder::getOrderNumber, orderNumber));
        if (Objects.isNull(seckillOrder)) {
            throw new YamiShopBindException("yami.data.deleted.or.not.exist");
        }
        return ServerResponseEntity.success(seckillOrder.getSeckillId());
    }
}
